<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" :visible="prop.open" width="800px" top="5vh" append-to-body :close-on-click-modal="false" :show-close="false">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="userList" @selection-change="handleSelectionChange" max-height="500px">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
      <el-button @click="cancel({isSaveData:false})">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { unallocatedUserList, addRoleUsers } from "@/api/system/role";
export default {
  dicts: ['sys_normal_disable'],
  props: {
    prop:{
      type:Object,
      default:function(){
        return {
          open:false,
          roleId:null,
        }
      }
    }
  },
  data() {
    return {
      userIds: [],
      // 总条数
      total: 0,
      userList: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        roleId: this.prop.roleId,
        userName: null,
      }
    };
  },
  methods: {
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.userIds = selection.map(item => item.userId);
    },
    // 查询表数据
    getList() {
      unallocatedUserList(this.queryParams).then(res => {
        this.userList = res.data.records;
        this.total = Number.parseInt(res.data.total);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择授权用户操作 */
    handleSelectUser() {
      const roleId = this.queryParams.roleId;
      if (this.userIds!==null && this.userIds.length>0) {
        let roleUsers = this.userIds.map(u =>{
          return {roleId:roleId,userId:u}
        })
        addRoleUsers(roleUsers).then(res => {
          this.$modal.msgSuccess(res.msg);
          this.cancel({isSaveData:true})
        });
      }else{
        this.$modal.msgError("请选择要分配的用户");
      }
    },
    cancel(saveParams) {
      this.$emit('close',saveParams)
    },
  },
  mounted() {
    this.getList()
  }
};
</script>
